import argparse
import sys
import pysam

def parse_args():
    parser = argparse.ArgumentParser("")
    parser.add_argument("--input", type=str,required=True)
    parser.add_argument("--pos", type=str,required=True)
    parser.add_argument("--neg", type=str,required=True)
    return parser.parse_args()

def main():
    args=parse_args()
    # 读取数据文件
    # 打开原始数据文件和写入文件
    input_bam = pysam.AlignmentFile(args.input, 'rb',check_sq=False)
    pos_num=0
    neg_num=0
    with open(args.pos, 'w') as pos_file,open(args.neg, 'w') as neg_file:
        # 创建一个集合来保存已经出现过的索引
        for read in input_bam:
            strand_code = 0 if read.is_reverse else 1
                    
            strand = "-" if strand_code==0 else "+"
            read_name=read.query_name
            if strand=="+":
                pos_file.write(read_name+'\n')
                pos_num+=1
            else:
                neg_file.write(read_name+'\n')
                neg_num+=1
    print('pos num: {}'.format(pos_num))
    print('neg num: {}'.format(neg_num))


if __name__ == '__main__':
    sys.exit(main())